﻿using System;
using System.Collections;
using System.Text.RegularExpressions;

namespace ASPItalia.ModelVirtualCasting
{
    public static class Extensions
    {
        public static bool IsNull(this object obj)
        {
            return obj == null;
        }

        public static bool IsNotNull(this object obj)
        {
            return obj != null;
        }

        public static bool IsNullOrEmpty(this Array obj)
        {
            if (obj.IsNotNull())
                return obj.Length.Equals(0);
            return true;
        }

        public static bool IsNullOrEmpty(this IList obj)
        {
            if (obj.IsNotNull())
                return obj.Count.Equals(0);
            return true;
        }

        public static bool IsMatch(this string obj, string regexPattern, RegexOptions options)
        {
            if (string.IsNullOrEmpty(regexPattern))
                return false;
            return Regex.IsMatch(obj, regexPattern, options);
        }

        public static bool IsMatch(this string obj, string regexPattern)
        {
            return obj.IsMatch(regexPattern, RegexOptions.None);
        }

        public static string Truncate(this string obj, int maxLength)
        {
            if (!string.IsNullOrEmpty(obj) && obj.Length > 3 && maxLength > 3)
                return obj.Length > maxLength ? obj.Substring(0, maxLength - 3) + "..." : obj;
            return obj;
        }
    }
}
